// subPages/health/recordFoot/recordFoot.js
var util =  require('../../../utils/util.js');
var recordJson = require('../../../data/recordJson');
let pages = getCurrentPages();
let currPage = pages[pages.length-1];
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    downImg: '/resources/down.png',
    upImg: '/resources/up.png',
    true: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setDate()
  },

  //设置日期
  setDate:function(){
    var that = this
    let date = util.setTime7(new Date())
    let Y = date.split('-')
    that.setData({
      date: date,
      dateStart: Y[0]- 3,
      dateEnd: date,
      dateImgsrc: this.data.downImg,
      footList: recordJson.footList
    })
    that.getData(date)
  },

  bindDateChange: function(e) {
    var that = this
    that.setData({
      date: e.detail.value,
      dateImgsrc: this.data.downImg
    })
    that.getData(that.data.date)
  },

  bindDateCancel:function(){
    this.setData({
      dateImgsrc: this.data.downImg
    })
  },

  //选择箭头
  imgChange:function(e){
    var that = this
    if(that.data.dateImgsrc == that.data.downImg){
      that.setData({
        dateImgsrc: that.data.upImg
      })
    }
  },

  //左选择日期
  leftDate:function(){
    var that = this
    that.setData({
      date: util.leftDate(that.data.date,2)
    })
    that.getData(that.data.date)
  },

  //右选择日期
  rightDate:function(){
    var that = this
    that.setData({
      date: util.rightDate(that.data.date,2)
    })
    that.getData(that.data.date)
  },


  //获得数据
  getData:function(date){
    console.log(date)
    var that = this
    wx.request({
      url: app.globalData.apiUrl +'/user_get_eat',
      data: {
        userid: app.globalData.userId,
        time: date,
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        console.log(res)
        that.setData({
          footList: recordJson.footList
        })
        that.processingData(res.data);
      },
      fail: function (res) {
        util.showModal('提示','数据异常')
      }
    })
  },

  //处理数据
  processingData:function(list){
    var that = this
    that.setData({
      footList: recordJson.footList2
    })
    if(list.length != 0){
      var footList = that.data.footList
      for(let i in list){
        switch (list[i].type) {
          case '1':
            footList[0].note = list[i].remark?list[i].remark:''
            footList[0].margin = list[i].remark?0:30
            footList[0].breakfastPicList = list[i].img_url?list[i].img_url:[]
            break;
          case '2':
            footList[1].note = list[i].remark?list[i].remark:''
            footList[1].margin = list[i].remark?0:30
            footList[1].lunchPicList = list[i].img_url?list[i].img_url:[]
            break;
          case '3':
            footList[2].note = list[i].remark?list[i].remark:''
            footList[2].margin = list[i].remark?0:30
            footList[2].dinnerPicList = list[i].img_url?list[i].img_url:[]
            break;
          case '4':
            footList[3].note = list[i].remark?list[i].remark:''
            footList[3].margin = list[i].remark?0:30
            footList[3].eattime = list[i].eattime?list[i].eattime:''
            footList[3].snacksPicList = list[i].img_url?list[i].img_url:[]
            break;
        }
      }
      that.setData({
        footList: footList
      })
    }
  },

  //改变日期
  changeData: function(date){
    var that = this
    that.setData({
      date: date
    })
    that.getData(that.data.date)
  },

  //减重管理
  management:function(e){
    var that = this
    wx.navigateTo({
      url: '../management/management?type=' + e.currentTarget.dataset.type + '&date=' +  e.currentTarget.dataset.date
    })
  },

  //历史记录
  historyBtn:function(){
    wx.navigateTo({
      url: '../footDate/footDate'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // var that = this
    // if (currPage.data.time){
    //   that.setData({//将携带的参数赋值
    //     date: currPage.data.time
    //   });
    //   that.getData(that.data.date)
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: 'PAPN健康管理',
      path: 'pages/index/index',
      success: function(res) {
        // res.shareTickets
      },
      fail: function(res) {
          console.log(res)
      }
    }
  }
})
